<template>
  <el-dialog
    ref="wkDialog"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    :title="title"
    width="405px"
    @close="handleCancel">
    <div class="handle-box">
      <el-form ref="form" :model="form" :rules="rules" label-width="105px">
        <el-form-item
          :label="$t('crm.invoice.593f34181df000e80528b6f08209b914')">
          <el-input v-model="form.invoiceNumber" />
        </el-form-item>
        <el-form-item
          :label="$t('crm.invoice.c8c4b0a548cc0abc079c8c5c82357354')"
          prop="realInvoiceDate">
          <el-date-picker
            v-model="form.realInvoiceDate"
            style="width: 100%;"
            value-format="yyyy-MM-dd"
            type="date"
            :placeholder="$t('crm.invoice.f7da74fb6cfe70535cc047f4d412ccfe')" />
        </el-form-item>
        <el-form-item
          :label="$t('crm.invoice.148e7a2e16758bbc2b616e1f03313120')">
          <el-input v-model="form.logisticsNumber" />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button v-debounce="handleConfirm" type="primary">
        {{ $t('crm.invoice.b5a7375e2aa7dbdc5f6a314d08ecf522') }}
      </el-button>
      <el-button @click.native="handleCancel">
        {{ $t('crm.invoice.f21612115d13e2b3423bc1c56d7fcbea') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { crmInvoiceStatusResetAPI, crmInvoiceStatusUpdateAPI } from '@/api/crm/invoice'

import ElDialogLoadingMixin from '@/mixins/ElDialogLoading'

export default {
  name: 'MarkInvoice', // 标记为开票
  components: {
  },
  mixins: [ElDialogLoadingMixin],
  props: {
    visible: {
      type: Boolean,
      required: true,
      default: false
    },
    detail: Object,
    reset: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        invoiceNumber: '',
        logisticsNumber: '',
        realInvoiceDate: ''
      },
      rules: {
        realInvoiceDate: [
          { required: true, message: this.$t('crm.invoice.b80a12e7e6f71ec8c1a872eb103d0d28'), trigger: 'change' }
        ]
      }
    };
  },
  computed: {
    title() {
      return this.reset ? this.$t('crm.invoice.8f6591cb1054c5e47b30f02be7ade763') : this.$t('crm.invoice.1791045c2d95bdd5046645241fda35a6');
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val && this.reset) {
          this.form = {
            invoiceNumber: this.detail.invoiceNumber,
            logisticsNumber: this.detail.logisticsNumber,
            realInvoiceDate: this.detail.realInvoiceDate
          }
        }
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    /**
     * 取消选择
     */
    handleCancel() {
      // 重置状态
      this.form = {
        invoiceNumber: '',
        logisticsNumber: '',
        realInvoiceDate: ''
      }
      this.$emit('update:visible', false)
    },

    /**
     * 点击确定
     */
    handleConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.invoiceNumber <= 0) {
            return
          }
          var params = {
            ...this.form
          }
          params.invoiceId = this.detail.invoiceId
          this.loading = true
          const request = this.reset ? crmInvoiceStatusResetAPI : crmInvoiceStatusUpdateAPI
          request(params)
            .then(res => {
              this.$message({
                type: 'success',
                message: this.$t('crm.invoice.e851b0bcb53c2950ec1cc2dbd2d7ee63')
              })
              this.loading = false
              this.detail.invoiceStatus = 1
              this.handleCancel()
              this.$emit('change')
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.handle-box {
  font-size: 12px;
  color: $--color-text-primary;
}

.handle-item {
  padding-bottom: 15px;

  .handle-item-name {
    flex-shrink: 0;
    width: 110px;
  }

  .handle-item-content {
    flex: 1;
  }
}

.handle-bar {
  position: relative;
  height: 30px;
  margin-top: 10px;

  button {
    float: right;
    margin-right: 10px;
  }
}
</style>
